package cn.hrlly.tao2shou.biz;

import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.callback.ApplicationCallback;
import cn.hrlly.tao2shou.domain.Application;
import cn.hrlly.tao2shou.domain.Status;
import cn.hrlly.tao2shou.listener.OnSubmitApplicationListener;
import okhttp3.Request;

/**
 * User: gengliang
 * Date: 2016-01-30
 * Time: 16:21
 */

public class SubmitApplicationBizImpl implements SubmitApplicationBiz {

    private OnSubmitApplicationListener onSubmitApplicationListener;

    public SubmitApplicationBizImpl(OnSubmitApplicationListener onSubmitApplicationListener){
        this.onSubmitApplicationListener=onSubmitApplicationListener;
    }

    @Override
    public void submitApplication(Application application,File file[]) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addFile(Config.key_file, Config.key_barter_file1, file[0])
                .addFile(Config.key_file,Config.key_barter_file2,file[1])
                .addFile(Config.key_file,Config.key_barter_file3,file[2])
                .addParams(Config.key_release_id,application.getReleaseId())
                .addParams(Config.key_expected_price, String.valueOf(application.getExpectedPrice()))
                .addParams(Config.key_trade_time,application.getTradeTime())
                .addParams(Config.key_trade_location,application.getTradeLocation())
                .addParams(Config.key_publisher_id,application.getPublisherId())
                .addParams(Config.key_barter_name,application.getBarterName())
                .addParams(Config.key_barter_description,application.getBarterDescription())
                .build()
                .execute(new ApplicationCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        onSubmitApplicationListener.submitApplicationFail(R.string.network_connection_error);
                    }

                    @Override
                    public void onResponse(Status status) {
                        if(status.getStatus()==0)
                            onSubmitApplicationListener.submitApplicationSucc(R.string.submit_application_success);
                        else
                            onSubmitApplicationListener.submitApplicationFail(status.getErrorMessage());
                    }
                });
    }
}
